package com.qf.service.impl;

import com.qf.dao.CSeatDetailMapper;
import com.qf.dao.DSeatDetailMapper;
import com.qf.pojo.po.CSeatDetail;
import com.qf.pojo.req.Ctrain;
import com.qf.pojo.req.Dtrain;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.resp.SeatInfo;
import com.qf.service.CSeatDetailService;
import com.qf.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class CSeatDetailServiceImpl implements CSeatDetailService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    CSeatDetailMapper cSeatDetailMapper;
    @Autowired
    DSeatDetailMapper dSeatDetailMapper;
    @Autowired
    UserUtils userUtils;

    @Override
    public BaseResp getTrain(Integer dispatureId, Integer distinationId) {
        if (dispatureId==distinationId){
            return new BaseResp(1,"暂无车票,请重新选择城市",null,null);
        }
        List<Ctrain> clist=new ArrayList<>();
        List<Dtrain> dlist=new ArrayList<>();

        Boolean c_train_list = redisTemplate.hasKey("C_TRAIN_LIST"+dispatureId+"_"+distinationId);
        if (!c_train_list){
            List<Ctrain> list= cSeatDetailMapper.getcTrain(dispatureId,distinationId);

            if (list.size()!=0){
                /* System.out.println("数据库c"+list);*/
                redisTemplate.opsForList().leftPushAll("C_TRAIN_LIST"+dispatureId+"_"+distinationId,list);
                clist=redisTemplate.opsForList().range("C_TRAIN_LIST"+dispatureId+"_"+distinationId,0,-1);
            }
        }else{
            clist=redisTemplate.opsForList().range("C_TRAIN_LIST"+dispatureId+"_"+distinationId,0,-1);
        }
        Boolean d_train_list = redisTemplate.hasKey("D_TRAIN_LIST"+dispatureId+"_"+distinationId);
        if (!d_train_list){
            List<Dtrain> list1=dSeatDetailMapper.getdTrain(dispatureId,distinationId);
            if (list1.size()!=0){
                /*System.out.println("数据库d"+list1);*/
                redisTemplate.opsForList().leftPushAll("D_TRAIN_LIST"+dispatureId+"_"+distinationId,list1);
                dlist=redisTemplate.opsForList().range("D_TRAIN_LIST"+dispatureId+"_"+distinationId,0,-1);
            }
        }else{
            dlist=redisTemplate.opsForList().range("D_TRAIN_LIST"+dispatureId+"_"+distinationId,0,-1);
        }
        if (clist.size()==0 && dlist.size()==0){
            return new BaseResp(1,"暂无车票",null,null);
        }
       /* System.out.println(clist);
        System.out.println(dlist);*/
        SeatInfo seatInfo = new SeatInfo(0, clist, dlist);
        return new BaseResp(0,"查询成功",null,seatInfo);
    }

    @Override
    public BaseResp getTrainMsg(String ctrainId, HttpServletRequest request) {
        if(ctrainId==null || ctrainId==""){
            return new BaseResp(1,"请重新选择",null,null);
        }
     /*   XcUser token = userUtil.getUserByToken(request.getHeader("token"));
        if (token==null){
            return new BaseResp(1,"对不起你还未登录,请先登录",null,null);
        }*/
        List<CSeatDetail> clist=new ArrayList<>();
        Boolean aBoolean = redisTemplate.hasKey("TRAIN_MSG" + ctrainId);
        if (!aBoolean){
            List<CSeatDetail> list = cSeatDetailMapper.getcTrainMsg(ctrainId);
            if (list.size()>0){
                redisTemplate.opsForList().leftPushAll("TRAIN_MSG" + ctrainId,list);
                clist=redisTemplate.opsForList().range("TRAIN_MSG" + ctrainId,0,-1);
            }
        }else{
            clist=redisTemplate.opsForList().range("TRAIN_MSG" + ctrainId,0,-1);
        }
        return new BaseResp(0,"查询成功",null,clist);
    }

    @Override
    public BaseResp getdTrainMsg(String dtrainId, HttpServletRequest request) {
        if(dtrainId==null || dtrainId==""){
            return new BaseResp(1,"请重新选择",null,null);
        }
     /*   XcUser token = userUtil.getUserByToken(request.getHeader("token"));
        if (token==null){
            return new BaseResp(1,"对不起你还未登录,请先登录",null,null);
        }*/
        List<CSeatDetail> dlist=new ArrayList<>();
        Boolean aBoolean = redisTemplate.hasKey("TRAIN_MSG" + dtrainId);
        if (!aBoolean){
            List<CSeatDetail> list = cSeatDetailMapper.getdTrainMsg(dtrainId);
            if (list.size()>0){
                redisTemplate.opsForList().leftPushAll("TRAIN_MSG" + dtrainId,list);
                dlist=redisTemplate.opsForList().range("TRAIN_MSG" + dtrainId,0,-1);
            }
        }else{
            dlist=redisTemplate.opsForList().range("TRAIN_MSG" + dtrainId,0,-1);
        }
        return new BaseResp(0,"查询成功",null,dlist);
    }
}
